package com.easy.service.impl;

import java.util.List;
import java.util.Map;

import com.easy.bean.Room;
import com.easy.dao.RoomDao;
import com.easy.service.Service;

public class RoomService implements Service{
	private RoomDao roomD = new RoomDao();
	
	public List<Room> queryList(Map params){
		String checktext=(String)params.get("checktext");
		String selecttext=(String)params.get("selecttext");
		String sql="select * from h_room where 1=1 ";
		try {
			if((!selecttext.equals(null)) && (!checktext.equals(null))) {
			sql+="and " + selecttext + " like " + "'%"+checktext+"%' ";
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(params.get("limit")!=null) {
			int limit=Integer.parseInt((String)params.get("limit"));
			int page=Integer.parseInt((String)params.get("page"));
			int start=(page-1)*limit;
			sql+="limit "+start+","+limit;
		}
		return roomD.querList(sql);
	}
	
	public Room query(int r_id) {
		String sql = "select * from h_room where r_id='" + r_id +"'";
		return roomD.query(sql);
	}

	@Override
	public Object query(Map params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int queryCount(Map params) {
		String checktext=(String) params.get("checktext");
		String sql = "select count(*) from h_room where 1=1";
		if(checktext != null) {
		sql+=" and r_id like '%"+checktext+"%' ";
		sql+=" or r_state like '%"+checktext+"%' ";
		sql+=" or r_type like '%"+checktext+"%' ";
		}
		return roomD.queryCount(sql);
	}

	@Override
	public int update(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("update h_room set ");
		strB.append("r_id='");
		strB.append(params.get("r_id"));
		strB.append("',r_state='");
		strB.append(params.get("r_state"));
		strB.append("',r_type='");
		strB.append(params.get("r_type"));
		strB.append("' where ");
		strB.append("r_id='");
		strB.append(params.get("r_id"));
		strB.append("'");
		int count = roomD.update(strB.toString());
		return count;
	}

	@Override
	public int delete(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("delete from h_room where r_id='");
		strB.append(params.get("r_id"));
		strB.append("'");
		int count = roomD.delete(strB.toString());
		return count;
	}

	@Override
	public int insert(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("insert into h_room(r_id,r_state,r_type) values(");
		strB.append("'");
		strB.append(params.get("r_id"));
		strB.append("','");
		strB.append(params.get("r_state"));
		strB.append("','");
		strB.append(params.get("r_type"));
		strB.append("'");
		strB.append(")");
		int count = roomD.insert(strB.toString());
		return count;
	}
}
